define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'product/index',
                    //add_url: 'product/add',
                    edit_url: 'product/edit',
                    del_url: Config.admin.nickname == 'admin' ? 'product/del' : '',
                    multi_url: 'product/multi',
                    dragsort_url: 'ajax/weigh',
                    table: 'product',
                }
            });
            var table = $("#table");
            var tableOptions = {
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'add_time',
                search: false,
                pageSize: 10,
                columns: [
                    [
                        {field: 'id', title: __('Id')},
                        {field: 'category', title: __('分类'), operate: false, searchList: Config.searchList, formatter: Table.api.formatter.label},
                        {field: 'title', title: __('标题')},
                        {field: 'video_img', title: __('视频封面'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'price', title: __('积分'), operate: false},
                        {field: 'weight', title: __('重量'), operate: false},
                        // {field: 'user_id', title: __('发布者ID')},
                        {field: 'mobile', title: __('手机号'), operate: false},
                        {field: 'user_id', title: __('发布者')},
                        {field: 'province', title: __('地址'), operate: false, formatter:function (value, row, index) {
                            return row.province+row.city;
                        }},
                        /* {field: 'position', title: __('交易范围'), operate: false, align: 'left', formatter:function (value, row, index) {
                                if(value == '0,1'){
                                    return '同城';
                                }else if(value == '1,1'){
                                    return '全国,同城';
                                }else if(value == '1,0'){
                                    return '全国';
                                }else{
                                    return '';
                                }
                            }
                        }, */
                        {field: 'is_contact', title: __('是否联系'), formatter: Table.api.formatter.status, searchList: {'0': '否', '1': '是'}},
                        {field: 'status', title: __('审核状态'), formatter: Table.api.formatter.status, searchList: {'0': '待审核', '1': '已上架','-1':'已下架'},width: 100},
                        {field: 'is_sale', title: __('交易状态'), formatter: Table.api.formatter.status, searchList: {'0': '未售出', '1': '已售出'},width: 100},
                        {field: 'add_time', title: __('创建时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            width: 200,
                            buttons: [
                                
                                {
                                    name: 'status',
                                    title: '沟通',
                                    text: '沟通',
                                    confirm: '是否确认已经沟通了？',
                                    classname: 'btn btn-warning btn-xs btn-magic btn-ajax',
                                    url: function (row) {
                                        return 'product/setIsContact?id='+row.id;
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.is_contact==0){
                                            return true;
                                        }else {
                                            return false;
                                        }
                                    },
                                    success: function (res){
                                        table.bootstrapTable('refresh');
                                    }
                                },
                                {
                                    name: '备注',
                                    text: __('备注'),
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: 'product/remark'
                                },
                                {
                                    name: 'status',
                                    title: '精选',
                                    text: '精选',
                                    confirm: '是否确认精选？',
                                    classname: 'btn btn-danger btn-xs btn-magic btn-ajax',
                                    url: function (row) {
                                        return 'product/setRecommendTime?type=1&id='+row.id;
                                    },
                                    visible: function (row) {
                                        if(Config.username == 'baozhong'){
                                            return false;
                                        }
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.recommend_time==0 && row.is_sale == 0){
                                            return true;
                                        }else {
                                            return false;
                                        }
                                    },
                                    success: function (res){
                                        table.bootstrapTable('refresh');
                                    }
                                },
                                {
                                    name: 'status',
                                    title: '取消精选',
                                    text: '取消精选',
                                    confirm: '是否确认取消精选？',
                                    classname: 'btn btn-primary btn-xs btn-magic btn-ajax',
                                    url: function (row) {
                                        return 'product/setRecommendTime?type=-1&id='+row.id;
                                    },
                                    visible: function (row) {
                                        if(Config.username == 'baozhong'){
                                            return false;
                                        }
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.recommend_time!=0  && row.is_sale == 0){
                                            return true;
                                        }else {
                                            return false;
                                        }
                                    },
                                    success: function (res){
                                        table.bootstrapTable('refresh');
                                    }
                                },
                                {
                                    name: 'status',
                                    title: '上架',
                                    text: '上架',
                                    confirm: '是否确认上架？',
                                    classname: 'btn btn-danger btn-xs btn-magic btn-ajax',
                                    url: function (row) {
                                        return 'product/setStatus?status=1&id='+row.id;
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if((row.status==0  || row.status==-1)  && row.is_sale == 0 ){
                                            return true;
                                        }else {
                                            return false;
                                        }
                                    },
                                    success: function (res){
                                        table.bootstrapTable('refresh');
                                    }
                                },
                                {
                                    name: 'status',
                                    title: '下架',
                                    text: '下架',
                                    confirm: '是否确认下架？',
                                    classname: 'btn btn-primary btn-xs btn-magic btn-ajax',
                                    url: function (row) {
                                        return 'product/setStatus?status=-1&id='+row.id;
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(!(row.status==0  || row.status==-1)  && row.is_sale == 0){
                                            return true;
                                        }else {
                                            return false;
                                        }
                                    },
                                    success: function (res){
                                        table.bootstrapTable('refresh');
                                    }
                                }
                            ],
                        }
                    ]
                ]
            };
            // 初始化表格
            table.bootstrapTable(tableOptions);

            // 为表格绑定事件
            Table.api.bindevent(table);

            //绑定TAB事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                // var options = table.bootstrapTable(tableOptions);
                var typeStr = $(this).attr("href").replace('#', '');
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    // params.filter = JSON.stringify({type: typeStr});
                    params.type = typeStr;

                    return params;
                };
                table.bootstrapTable('refresh', {});
                return false;

            });

            $('.commonsearch-table').removeClass('hidden');

        },
        add: function () {
            Controller.api.bindevent();
            setTimeout(function () {
                $("#c-type").trigger("change");
            }, 100);
        },
        edit: function () {
            Controller.api.bindevent();
        },
        remark: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                $(document).on("change", "#c-type", function () {
                    $("#c-pid option[data-type='all']").prop("selected", true);
                    $("#c-pid option").removeClass("hide");
                    $("#c-pid option[data-type!='" + $(this).val() + "'][data-type!='all']").addClass("hide");
                    $("#c-pid").data("selectpicker") && $("#c-pid").selectpicker("refresh");
                });
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
